package com.prs.crm.domain.stock;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.hibernate.annotations.Cascade;

import com.prs.crm.domain.FlowCodeEntity;
import com.prs.crm.domain.crm.Customer;

/**
 * 物料
 * @author courser.tijichen
 */
@Entity
@Table(name = "stock_product")
public class Product extends FlowCodeEntity{

	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer id;

	private String name;

	/**
	 * 单位
	 */
	@ManyToOne
	private ProductUnit  productUnit;
	/**
	 * 单位
	 */
	 private String unit;

	@OneToOne
	private ProductStore productstore;
	@ManyToOne
	private ProductClass classifi;
	@ManyToOne
	private Customer customer;



	public Customer getCustomer() {
		return customer;
	}



	public void setCustomer(Customer customer) {
		this.customer = customer;
	}

	/**
	 * 规格
	 */
	private String norm;

	/**
	 * 计划单价
	 */
	private Double planPrice;

	/**
	 * 市场单价
	 */
	private Double price;
	/**
	 * 产品编码
	 */
	private String productcode;


	@Transient
	private Map<String, String> valueMap;

	/**
	 * 自定义属性的值
	 */
	@OneToMany(mappedBy = "product", fetch = FetchType.LAZY)
	@Cascade( { org.hibernate.annotations.CascadeType.ALL,
			org.hibernate.annotations.CascadeType.DELETE_ORPHAN })
	private Set<ProductAttrValue> values;

	@Lob
	private String remark;
	
	/**
	 * 批号
	 */
	
	private String batchNum;
	
	private boolean isAlert;

	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getId() {
		return id;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getName() {
		return name;
	}

	public void setValues(Set<ProductAttrValue> values) {
		this.values = values;
	}

	public Set<ProductAttrValue> getValues() {
		return values;
	}


	public void setClassifi(ProductClass classifi) {
		this.classifi = classifi;
	}

	public ProductClass getClassifi() {
		return classifi;
	}

	public void setNorm(String norm) {
		this.norm = norm;
	}

	public String getNorm() {
		return norm;
	}

	public void setPlanPrice(Double planPrice) {
		this.planPrice = planPrice;
	}

	public Double getPlanPrice() {
		return planPrice;
	}

	public void setPrice(Double price) {
		this.price = price;
	}

	public Double getPrice() {
		return price;
	}

	public void setRemark(String remark) {
		this.remark = remark;
	}

	public String getRemark() {
		return remark;
	}

	public Map<String, String> getValuesMap() {
		if (valueMap == null) {
			valueMap = new HashMap<String, String>();
			if (this.values != null) {
				for (ProductAttrValue value : values) {
					valueMap.put(value.getAttr().getId().toString(), value
							.getValue());
				}
			}
		}
		return valueMap;
	}

	public ProductAttrValue getAttrValue(ClassAttribute attr) {
		if (this.values != null) {
			for (ProductAttrValue value : values) {
				if (value.getAttr().getId().equals(attr.getId()))
					return value;
			}
			return null;
		} else {
			return null;
		}
	}

	public void setAttrValue(ClassAttribute attr, String value) {

		ProductAttrValue val = new ProductAttrValue();
		val.setProduct(this);
		val.setAttr(attr);
		val.setValue(value);
		if (this.values != null) {
			this.values.clear();
		} else {
			this.setValues(new HashSet<ProductAttrValue>());
		}
		this.getValues().add(val);
	}

	public void addAttrValue(String value) {
		ProductAttrValue val = new ProductAttrValue();
		val.setProduct(this);
		val.setValue(value);
		if (this.values == null) {
			this.setValues(new HashSet<ProductAttrValue>());
		}
		this.getValues().add(val);
	}

	public ProductStore getProductstore() {
		return productstore;
	}

	public void setProductstore(ProductStore productstore) {
		this.productstore = productstore;
	}
	

	public String getProductcode() {
		return productcode;
	}



	public void setProductcode(String productcode) {
		this.productcode = productcode;
	}



	public ProductUnit getProductUnit() {
		return productUnit;
	}

	public void setProductUnit(ProductUnit productUnit) {
		this.productUnit = productUnit;
	}

	public String getUnit() {
		return unit;
	}
	public void setUnit(String unit){
		this.unit=unit;
	}

	public Map<String, String> getValueMap() {
		return valueMap;
	}

	public void setValueMap(Map<String, String> valueMap) {
		this.valueMap = valueMap;
	}

	public String getBatchNum() {
		return batchNum;
	}

	public void setBatchNum(String batchNum) {
		this.batchNum = batchNum;
	}

	public boolean isAlert() {
		return isAlert; 
		
	}

	public void setAlert(boolean isAlert) {
		this.isAlert = isAlert;
	}

}
